package com.meeting.meeting.controller;

import com.github.pagehelper.PageInfo;
import com.meeting.meeting.model.Department;
import com.meeting.meeting.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 何星霖
 * @date 2019/12/28 20:47
 */
@Controller
public class DepartmntController {
    @Autowired
    DepartmentService departmentService;
    @RequestMapping("/getAllDept")
    public String getAllDept(Department department, ModelMap model,
                             @RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
                             @RequestParam(required=true,value="pageSize",defaultValue="10") Integer pageSize){
        model.put("department",department);
        PageInfo<Department> pageInfo = departmentService.selectAll(pageNum,pageSize,department);
        List<Department> list=pageInfo.getList();
        long count=pageInfo.getTotal();
        model.put("deptList",list);
        model.put("pageInfo",pageInfo);
        model.put("count",count);
        return "pages/department";
    }
    @RequestMapping("/deletdept")
    public String delete(Integer id,ModelMap model){
        int a=departmentService.deleteByPrimaryKey(id);
        if (a>0){
            model.put("type","dept");
            return "pages/delete";
        }else {
            model.put("tips","过程出现不安定因素，请重试!");
            return "pages/505";
        }
    }
    @RequestMapping("/getdeptbyid")
    public String getdeptbyid(@RequestParam(required=true,value="id") Integer id,ModelMap model){
        Department department=departmentService.selectByPrimaryKey(id);
        model.put("dept",department);
        return "pages/deptedit";
    }
    @RequestMapping("/editdept")
    public String editdept(ModelMap model, Department department){
        int a=departmentService.updateByPrimaryKey(department);
        if (a>0){
            model.put("type","dept");
            return "pages/update";
        }else {
            model.put("tips","过程出现不安定因素，请重试!");
            return "505";
        }
    }
    @RequestMapping("/newdept")
    public String newdept(ModelMap model, Department department){
        int a=departmentService.insert(department);
        if (a>0){
            model.put("type","dept");
            return "pages/new";
        }else {
            model.put("tips","过程出现不安定因素，请重试!");
            return "505";
        }
    }
    @RequestMapping("/tonewdept")
    public String tonewdept(ModelMap model, Department department){
       return "pages/deptnew";
    }
}
